import React from "react";
import yhTools from "@tools/yh";
import { InterfaceG } from "@config/Config";
import { Toast } from "antd-mobile";

/**
 * 人脸高阶组件----公共业务抽离
 */
const livenessHOC = () => WrapComponent =>
	class Index extends React.Component {
		constructor(props) {
			super(props);
			this.state = {};
		}

		/**************************************人脸1.0.6版本start ************************** */

		/**获取face++的token */
		async face_getFaceBizToken() {
			Toast.loading("加载中...", 100);
			let objs = {
				url: InterfaceG.face_getFaceBizToken,
				params: { productNo: "601" }
			};
			let resp = await yhTools.request(objs);
			Toast.hide();
			if (resp.code === "0") {
				const token = resp.data.bizToken;
				return this.startLiveFace106(token);
			} else {
				Toast.info(resp.message, 2);
				return resp;
			}
		}
		/**
		 * 开始活体 1.0.6版本开始使用
		 * @param {*} props
		 * @param {*} token
		 */
		async startLiveFace106(bizToken) {
			const { authHeader, userName, idNo, lnid, applyNo } = this.props;
			let params = {
				needUpload: true,
				url: InterfaceG.face_authAndSubmitCreditV2,
				header: authHeader, //头部信息
				bizToken,
				params: {
					userName,
					idNo,
					lnid,
					applyNo,
					productNum: "601",
					version: "1.0",
					bizToken,
					productNo: "601"
				},
				_alertShow: false
			};
			let resp = await yhTools.startLiveFace(params);
			return resp;
		}
		/**************************************人脸1.0.6版本end ************************** */

		/**
		 * 提交授信
		 */
		/**科罗纳调授信接口 */
		xhfAppSubApply = () => {
			const { lnid, applyNo, devicesInfo, history } = this.props;
			let objs = {
				url: InterfaceG.yhbXhfApply,
				params: {
					productNum: "601",
					userDeviceInfo: JSON.stringify(devicesInfo)
				}
			};
			yhTools.request(objs).then(res => {
				if (res.code === "0") {
					const status = res.data.status;
					if (status == "1") {
						history.push({
							pathname: "/xhfHomeCredit"
						});
					} else if (status == "4") {
						history.push({
							pathname: "/xhfOpenFail",
							state: { message: res.data.message }
						});
					} else if (status == "2" || status == "3") {
						//授信中
						//科罗纳去10秒倒计时页面
						history.push("/xhfLivenessRet");
					}
				} else {
					Toast.info(res.message, 2);
				}
			});
		};

		/**
		 * 617流程迁移后调授信应用的人脸接口
		 */
		startLiveFace617 = () => {
			console.log("场景1。。。");
			//1.0.6版本替换Face++
			if (yhTools.isHigherVersion()) {
				// 1.0.6版本
				return this.face_getFaceBizToken();
			} else {
				// 1.0.6以下
				return this.startLiveFace617Req();
			}
		};
		async startLiveFace617Req() {
			const {
				authHeader,
				userName,
				idNo,
				lnid,
				applyNo,
				devicesInfo
			} = this.props;
			let params = {
				needUpload: true,
				url: InterfaceG.faceAuth,
				header: authHeader, //头部信息
				params: {
					userName,
					idNo,
					lnid,
					channelCode: "YHB",
					applyNo,
					productNo: "601",
					userDeviceInfo: JSON.stringify(devicesInfo)
				},
				_alertShow: false
			};
			let resp = await yhTools.startLiveFace(params);
			return resp;
		}
		async startLiveFaceThreeInfo() {
			const { authHeader } = this.props;
			let params = {
				needUpload: true,
				url: "/app/identityAuth/faceFdentify",
				header: authHeader, //头部信息
				params: {
					channelCode: "YHB",
					userDeviceInfo: JSON.stringify(
						JSON.parse(sessionStorage.getItem("getAppPlatformsDatas"))
							.devicesInfo
					),
					longitude: JSON.parse(sessionStorage.getItem("getAppPlatformsDatas"))
						.longitude,
					latitude: JSON.parse(sessionStorage.getItem("getAppPlatformsDatas"))
						.latitude
				},
				_alertShow: false
			};
			let resp = await yhTools.startLiveFace(params);
			return resp;
		}
		startLiveFaceThree = () => {
			return this.startLiveFaceThreeInfo();
		};
		componentWillUnmount() {}
		render() {
			return (
				<WrapComponent
					{...this.props}
					livenessHOC={{
						startLiveFace617: this.startLiveFace617,
						xhfAppSubApply: this.xhfAppSubApply,
						startLiveFaceThree: this.startLiveFaceThree
					}}
				/>
			);
		}
	};
export default livenessHOC;
